#include "userMain.h"

uint8 userBeepEnable = 1;
uint8 userBeepVolumeLevel = 1;
uint8 userBeepVolumeNow = 0;
uint8 userBeepOperateSrc = 0;

void userBeepInitFunc(void)
{
    PWM_Beep_Start();
    userBeepVolumeSetFunc(userBeepVolumeLevel);
    userBeepOff();
    //userBeepModify(30);
}

void userBeepOn(void)
{
   userBeepModify(userBeepVolumeNow);
}

void userBeepOff(void)
{
    userBeepModify(0x00);
}

void userBeepModify(unsigned char beepValue)
{
    unsigned int userValue;
    userValue=beepValue*255;
    PWM_Beep_Start();
    PWM_Beep_WriteCompare(userValue);
    //PWM_Beep_WriteCompare(2000);
}

#define     BEEP_VOL_HIGH       7
#define     BEEP_VOL_LOW        1
void userBeepVolumeSetFunc(unsigned char beepVolume)
{
    if(beepVolume)
    {
        userBeepVolumeNow = BEEP_VOL_HIGH;
		userBeepVolumeLevel = 1;
    }
    else
    {
        userBeepVolumeNow = BEEP_VOL_LOW;
		userBeepVolumeLevel = 0;
    }
}

void userBeepEnableFunc(unsigned char bEnableValue)
{
    if(bEnableValue)
    {
        userBeepEnable = 1;
    }
    else
    {
        userBeepEnable = 0;
    }
}

#define     BEEP_LONG_TIME      1000
#define     BEEP_SHORT_TIME     200

unsigned char userBeepCnt = 2;
unsigned char userBeepMode = 1;
unsigned int userBeepMarkTick=0;
unsigned char userBeepFlag = 0;
void userBeepMarkTickFunc(void)
{
    if(userBeepMarkTick < 5000)
    {
        userBeepMarkTick++;
    }
}

void userBeepSetFunc(unsigned char beepMode,unsigned char beepCnt)
{
    if(userBeepEnable == 0)return ;
    userBeepMode = beepMode;
    userBeepCnt = beepCnt;
    userBeepFlag = 1;
}

extern unsigned char userAlarmFlag;
void userBeepOperateFunc(void)
{
    static unsigned char beepStep=0;
    static unsigned int userBeepTime=0;

//	PWM_Beep_WritePeriodBuf(1000);
    if(userAlarmFlag) return;
    if(userBeepEnable == 0) 
    {
        userBeepFlag = 0;
        return ;
    }
    if(userBeepFlag==0 ) return ;
    if(userBeepFlag==1)
    {
       if(userBeepMode)
       {
           userBeepTime = BEEP_SHORT_TIME;
       }
       else
       {
           userBeepTime = BEEP_LONG_TIME;
       }
       beepStep = 1; 
       userBeepFlag = 2;
    }
    switch(beepStep)
    {
        case 0x01:
        {
            userBeepOn();
            userBeepMarkTick=0;
            beepStep = 2;
            break;
        }
        case 0x02:
        {
            if(userBeepMarkTick <= userBeepTime) return ;
            userBeepMarkTick = 0;
            userBeepOff();
            beepStep = 3;
            break;
        }
        case  0x03:
        {
            if(userBeepMarkTick <= userBeepTime) return ;
            userBeepCnt--;
            if(userBeepCnt)
            {
                beepStep = 1;
            }
            else
            {
                userBeepFlag = 0;
            }
            break;
        }
        default:
        {
            break;
        }
    }
}

void userBeepCtrlFunc(unsigned char userBeepIndex, unsigned char userOperateMode)
{
    if(userBeepIndex==1)
    {
        userBeepVolumeSetFunc(userOperateMode);
    }
    if(userBeepIndex==0)
    {
        if(userOperateMode)
        {
            userBeepOn();//userBeepEnable = 1;
        }
        else
        {
            userBeepOff();//userBeepEnable = 0;
        }
    }
}